package com.book.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.common.entity.QueryRequest;
import com.book.system.entity.Money;
import com.book.system.mapper.MoneyMapper;
import com.book.system.service.MoneyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MoneyServiceImpl extends ServiceImpl<MoneyMapper, Money> implements MoneyService {
    @Autowired
    MoneyMapper moneyMapper;

    @Override
    public List<Money> getMoney(Money money) {
        QueryWrapper<Money> queryWrapper = new QueryWrapper<>();
        return this.moneyMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<Money> getMoneyByPage(Money money, QueryRequest request) {
        QueryWrapper<Money> queryWrapper = new QueryWrapper<>();
        if (money.getCreatetime() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            queryWrapper.like("createtime",simpleDateFormat.format(money.getCreatetime()));
        }
        if (StringUtils.isNotEmpty(money.getType())) {
            queryWrapper.eq("type", money.getType());
        }
        if (StringUtils.isNotEmpty(money.getUsername())) {
            queryWrapper.eq("username", money.getUsername());
        }
        if (StringUtils.isNotEmpty(money.getOmit())) {
            queryWrapper.eq("omit", money.getOmit());
        }
        Page<Money> moneyIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(moneyIPage, queryWrapper);
    }

    @Override
    @Transactional
    public void addMoney(Money money) {
        this.save(money);
    }

    @Override
    @Transactional
    public void updateMoney(Money money) {
        this.saveOrUpdate(money);
    }

    @Override
    @Transactional
    public void deletePay(String[] money) {
        List<String> list = Arrays.asList(money);
        this.removeByIds(list);
    }

    @Override
    public List<Money> deleteMoney(String[] money) {
        List<String> list = Arrays.asList(money);
        return (List<Money>) listByIds(list);
    }
}
